package cn.iocoder.yudao.module.scrm.controller.admin.operaterecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.scrm.controller.admin.operaterecord.vo.*;
import cn.iocoder.yudao.module.scrm.dal.dataobject.operaterecord.OperateRecordDO;
import cn.iocoder.yudao.module.scrm.service.operaterecord.OperateRecordService;

@Tag(name = "管理后台 - 操作记录")
@RestController
@RequestMapping("/scrm/operate-record")
@Validated
public class OperateRecordController {

    @Resource
    private OperateRecordService operateRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建操作记录")
    @PreAuthorize("@ss.hasPermission('scrm:operate-record:create')")
    public CommonResult<Integer> createOperateRecord(@Valid @RequestBody OperateRecordSaveReqVO createReqVO) {
        return success(operateRecordService.createOperateRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新操作记录")
    @PreAuthorize("@ss.hasPermission('scrm:operate-record:update')")
    public CommonResult<Boolean> updateOperateRecord(@Valid @RequestBody OperateRecordSaveReqVO updateReqVO) {
        operateRecordService.updateOperateRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除操作记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:operate-record:delete')")
    public CommonResult<Boolean> deleteOperateRecord(@RequestParam("id") Integer id) {
        operateRecordService.deleteOperateRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:operate-record:query')")
    public CommonResult<OperateRecordRespVO> getOperateRecord(@RequestParam("id") Integer id) {
        OperateRecordDO operateRecord = operateRecordService.getOperateRecord(id);
        return success(BeanUtils.toBean(operateRecord, OperateRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得操作记录分页")
    @PreAuthorize("@ss.hasPermission('scrm:operate-record:query')")
    public CommonResult<PageResult<OperateRecordRespVO>> getOperateRecordPage(@Valid OperateRecordPageReqVO pageReqVO) {
        PageResult<OperateRecordDO> pageResult = operateRecordService.getOperateRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OperateRecordRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出操作记录 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:operate-record:export')")
    @OperateLog(type = EXPORT)
    public void exportOperateRecordExcel(@Valid OperateRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OperateRecordDO> list = operateRecordService.getOperateRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "操作记录.xls", "数据", OperateRecordRespVO.class,
                        BeanUtils.toBean(list, OperateRecordRespVO.class));
    }

}